package app.v1.wdt.service.impl;

import app.v1.wdt.domain.pojo.WdtSalesOrderIdGenerator;
import app.v1.wdt.mapper.WdtSalesOrderIdGeneratorMapper;
import app.v1.wdt.service.WdtSalesOrderIdGeneratorService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * @author feizns
 * @since 2023/7/19
 */
@Service
@RequiredArgsConstructor
public class WdtSalesOrderIdGeneratorServiceImpl implements WdtSalesOrderIdGeneratorService {

    /**
     * 映射器
     */
    private final WdtSalesOrderIdGeneratorMapper mapper;

    @Override
    public Long nextId(String shopNo, String warehouseNo, LocalDate date) {
        Long nextId = mapper.nextId(shopNo, warehouseNo, date);
        if ( nextId == null )  {
            WdtSalesOrderIdGenerator order = new WdtSalesOrderIdGenerator();
            order.setShopNo(shopNo);
            order.setWarehouseNo(warehouseNo);
            order.setDate(date);
            order.setNextId(1L);
            order.insert();
            nextId = 0L;
        } else {
            mapper.plusNextId(shopNo, warehouseNo, date);
        }
        return nextId;
    }

}
